package mybatis.mapper;

import mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    @ResultMap("resultMap")
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();

    @Select("select * from userinfo where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoByDeleteFlag(Integer flag);

    @Select("select * from userinfo where delete_flag = ${deleteFlag}")
    List<UserInfo> getUserInfoByDeleteFlag2(Integer flag);

    @Select("select * from userinfo where delete_flag = #{deleteFlag} and gender = #{gender}")
    List<UserInfo> getUserInfoAll2(Integer deleteFlag, Integer gender);

    @Select("select * from userinfo where delete_flag = #{param1} and gender = #{gender}")
    List<UserInfo> getUserInfoAll3(Integer deleteFlag, Integer gender);

    @Select("select * from userinfo where delete_flag = #{deleteFlag} and gender = #{gender}")
    List<UserInfo> getUserInfoAll4(@Param("deleteFlag") Integer flag, Integer gender);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username, password, age, gender) " +
            "values (#{username}, #{password}, #{age}, #{gender})")
    Integer insert(UserInfo userInfo);

    @Insert("insert into userinfo (username, password, age, gender) " +
            "values (#{user.username}, #{user.password}, #{user.age}, #{user.gender})")
    Integer insert2(@Param("user") UserInfo userInfo);

    @Delete("delete from userinfo where id > #{id}")
    Integer delete(Integer id);

    @Update("update userinfo set password = #{password}, age = #{age}, gender = #{gender} where id = #{id}")
    Integer update(UserInfo userInfo);

    @Select("select * from userinfo where username = #{username}")
    UserInfo getUserInfoByName(String username);

    @Select("select * from userinfo where username = '${username}'")
    UserInfo getUserInfoByName2(String username);

    @Select("select id, username, password, age, gender, phone,  " +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userinfo")
    List<UserInfo> selectUserInfos();

    @Results(id = "resultMap",value = {
        @Result(column = "delete_flag" , property = "deleteFlag"),
        @Result(column = "create_time" , property = "createTime"),
        @Result(column = "update_time" , property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> selectUserInfos2();

    @Select("select * from userinfo")
    List<UserInfo> selectUserInfos3();

}
